package com.magicsoft.app.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.boyuan.huijiaoyu.R;
import com.boyuan.huijiaoyu.SettingPolicyActivity;
import com.magicsoft.app.entity.MenuItemEntity;
import com.magicsoft.app.helper.StringUtils;
import com.magicsoft.app.helper.ToastHelper;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class GridAdapter extends CTHAdapter<MenuItemEntity> {

	// private DisplayImageOptions options;
	// private ImageLoader imageLoader;
	private int height;

	public GridAdapter(Context context, List<MenuItemEntity> data,
			DisplayImageOptions options, ImageLoader imageLoader,
			int screenWidth) {
		super(context, data);
		// this.options = options;
		// this.imageLoader = imageLoader;
		this.height = screenWidth / 4 * 100 / 120;
	}

	@Override
	public int getCount() {

		return data.size();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		HolderViewStatic viewContent = new HolderViewStatic();

		MenuItemEntity menuItemEntity = data.get(position);

		if (convertView == null) {

			convertView = mInflater.inflate(R.layout.view_group_item, null);

			viewContent.linearlayout = (LinearLayout) convertView
					.findViewById(R.id.linearlayout);

			viewContent.imageView = (ImageView) convertView
					.findViewById(R.id.imageview);

			viewContent.textView = (TextView) convertView
					.findViewById(R.id.textview);

			convertView.setTag(viewContent);

		} else {
			viewContent = (HolderViewStatic) convertView.getTag();
		}

		ViewGroup.LayoutParams params = viewContent.linearlayout
				.getLayoutParams();
		params.height = height;
		viewContent.linearlayout.setLayoutParams(params);

		final String name = menuItemEntity.getName();
		final String act = menuItemEntity.getAct();
		int img = menuItemEntity.getImg();
		final String url = menuItemEntity.getUrl();
		final String proto = menuItemEntity.getProto();

		viewContent.imageView.setBackgroundResource(img);

		// if (StringUtils.isImageUrl(img)) {
		// imageLoader.displayImage(img, viewContent.imageView, options,
		// new ImageLoadingListener() {
		//
		// @Override
		// public void onLoadingStarted(String imageUri, View view) {
		// // TODO Auto-generated method stub
		// }
		//
		// @Override
		// public void onLoadingFailed(String imageUri, View view,
		// FailReason failReason) {
		// // TODO Auto-generated method stub
		// if (view != null) {
		// int viewWith = height - 50;
		//
		// LayoutParams layoutParams = (LayoutParams) view
		// .getLayoutParams();
		// layoutParams.width = viewWith;
		// layoutParams.height = viewWith;
		//
		// view.setLayoutParams(layoutParams);
		//
		// }
		// }
		//
		// @Override
		// public void onLoadingComplete(String imageUri,
		// View view, Bitmap loadedImage) {
		// // TODO Auto-generated method stub
		// if (view != null && loadedImage != null) {
		//
		// int viewWith = height - 50;
//		int imgHeight = loadedImage.getHeight();
//		if (viewWith < imgHeight) {
//
//			LayoutParams layoutParams = new LayoutParams(
//					viewWith, viewWith);
//			view.setLayoutParams(layoutParams);
//		}else {
//			LayoutParams layoutParams = new LayoutParams(
//					imgHeight, imgHeight);
//			view.setLayoutParams(layoutParams);
//		}
		// }
		// }
		//
		// @Override
		// public void onLoadingCancelled(String imageUri,
		// View view) {
		// // TODO Auto-generated method stub
		// }
		// });
		// } else {
		// viewContent.imageView.setImageDrawable(null);
		// }

		viewContent.textView.setText(StringUtils.isNotEmpty(name) ? name : "");

		viewContent.linearlayout.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				if ("url".equals(act)) {
					outToWebView(url, name);
				} else if ("proto".equals(act)) {
					// 内部应用
					try {
						if (StringUtils.isNotEmpty(proto)) {
							Intent intent = new Intent(context, Class
									.forName(proto.trim()));
							intent.putExtra("title", name);
							context.startActivity(intent);
						} else {
							ToastHelper.showMsg(context, "服务器数据有误", false);
						}
					} catch (ClassNotFoundException e) {
						ToastHelper.showMsg(context, "服务器数据有误", false);
					}
				}

			}
		});
		return convertView;

	}

	static class HolderViewStatic {
		LinearLayout linearlayout;
		ImageView imageView;
		TextView textView;

	}

	private void outToWebView(String website, String title) {

		// Uri uri = Uri.parse(website);
		// Intent intent = new Intent(Intent.ACTION_VIEW, uri);
		// context.startActivity(intent);

		Intent intent = new Intent(context, SettingPolicyActivity.class);
		intent.putExtra("addr", website);
		intent.putExtra("title", title);
		intent.putExtra("isloading", 0);
		context.startActivity(intent);
	}
}
